<?php 
	namespace app\admin\controller;

	use think\Controller;
	use app\model\User;
	use think\Session;
	use think\Db;

	class Userdo extends Controller{

		public function  _initialize(){
			if(!session::has("admin_id")){
				return $this->error("请登录","index/index");
			}
		}
		
		// 获取用户列表
		public function userList(){
			// $users = User::where()->paginate(10);
			$users = User::getListByPage(10);
			$this->assign("users",$users);
			// dump($users);
			return $this->fetch();
		}

		// 获取已删除用户列表
		public function deleteUserList(){
			// $users = User::where()->paginate(10);
			$users = User::getDeleteByPage(10);
			$this->assign("users",$users);
			// dump($users);
			return $this->fetch();
		}

		// 添加用户
		public function doUserAdd(){
			$pars = input('post.');
			$user = User::get(['user_name'=>$pars['user_name']]);
			if(null == $user){
				$pars['user_id'] = getId();
				$pars['user_password'] = md5($pars['user_password']);
				User::create($pars);
				$user = User::get($pars['user_id']);
				return getResponse(0,$user);
			}else{
				return getResponse(1,"该用户已存在");
			}
		}

		// 删除用户
		public function doUserDele(){
			$res = Db::name('user')->where("user_id",input('user_id'))->update(['is_delete'=>1]);
			if($res){
				return getResponse(0,input('user_id'));
			}
			return getResponse(1,"删除失败");
		}

		// 恢复用户
		public function doUserRecovery(){
			$res = Db::name('user')->where("user_id",input('user_id'))->update(['is_delete'=>0]);
			if($res){
				return getResponse(0,input('user_id'));
			}
			return getResponse(1,"恢复失败");
		}

		// 重置用户密码
		public function resetPassword(){
			$res = Db::name('user')->where("user_id",input('user_id'))->update(['user_password'=>md5("123456")]);
			if($res){
				return getResponse(0,input('user_id'));
			}
			return getResponse(1,"重置密码失败");
		}

		// 启用用户
		public function doUserEnable(){
			$res = Db::name('user')->where("user_id",input('user_id'))->update(['status'=>0]);
			if($res){
				echo json_encode(["user_id"=>input('user_id')]);
			}else{
				$this->error('修改失败');
			}
		}

		// 禁用用户
		public function doUserDisable(){
			$res = Db::name('user')->where("user_id",input('user_id'))->update(['status'=>1]);
			if($res){
				echo json_encode(["user_id"=>input('user_id')]);
			}else{
				$this->error('修改失败');
			}
		}

		// 获取用户详细信息
		public function getUser(){
			$res = User::getInfo(input("user_id"));
			return getResponse(0,$res);
		}

		// 查找用户
		public function findUser(){

		}

		// 用户充值
		public function Recharge(){
			$user = User::get(input('user_id'));
			if(null == $user){
				return getResponse(1,"充值失败!");
			}else{
				$user->user_balance += input('recharge');
				$user->save();
				return getResponse(0,$user);
			}
		}
	}
 ?>